package com.huang825172.sbog.GlobalManager;

import android.content.Context;
import android.content.SharedPreferences;
import android.nfc.Tag;
import android.util.Log;

import com.google.gson.Gson;
import com.huang825172.sbog.GlobalManager.Action.ActionCreator;
import com.huang825172.sbog.GlobalManager.Datas.checkToken;
import com.huang825172.sbog.GlobalManager.Datas.checkTokenRes;
import com.huang825172.sbog.R;
import com.huang825172.sbog.Tabs.Mine.LoginPage.Store.Datas.Login;
import com.huang825172.sbog.Util.StatusCacheUtil;
import com.huang825172.sbog.Util.TAGs;

import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class LoginStateManager {
    private Context context;
    private StatusCacheUtil cache;
    private ActionCreator actionCreator;

    public LoginStateManager(Context context) {
        this.context = context;
        cache = new StatusCacheUtil(context.getResources().getString(R.string.loginStatusCache), context);
        actionCreator = new ActionCreator();
    }

    public String getLoginToken() {
        return cache.getAsString("token");
    }

    public String getLoginPhone() {
        return cache.getAsString("loginPhone");
    }

    public void quitLogin(){
        cache.put("token","");
        cache.put("loginPhone","");
    }

    public void checkToken(String token) {
        checkToken info = new checkToken(token);
        final Gson gson = new Gson();
        String infoStr = gson.toJson(info);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(context.getResources().getString(R.string.server_baseUrl))
                .build();
        Interfaces request = retrofit.create(Interfaces.class);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), infoStr);
        Call<ResponseBody> call = request.checkToken(body);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String res = response.body().string();
                    Log.d(TAGs.INFO, res);
                    checkTokenRes resObj = gson.fromJson(res, checkTokenRes.class);
                    if (resObj.code == 200) {
                        actionCreator.createCheckTokenAction(true);
                    } else {
                        actionCreator.createCheckTokenAction(false);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.e(TAGs.ERROR, e.getMessage());
                    actionCreator.createCheckTokenAction(false);
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                t.printStackTrace();
                Log.e(TAGs.ERROR, t.getMessage());
                actionCreator.createCheckTokenAction(false);
            }
        });
    }
}
